package com.site.action.backend.dining;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.site.dto.dining.RestaurantMenuBizBean;
import com.site.service.dining.RestaurantMenuService;
import com.site.util.base.action.BaseAction;
import com.site.util.base.service.BaseService;

/**
 * 餐厅菜单
 * @author win7
 * 
 *
 */
public class RestaurantMenuAction extends BaseAction {
 
	private static final long serialVersionUID = 1L;

	private RestaurantMenuBizBean restaurantMenuBizBean;
	
	private List<RestaurantMenuBizBean> restaurantMenuBizBeans;
	
	public List<RestaurantMenuBizBean> getRestaurantMenuBizBeans() {
		return restaurantMenuBizBeans;
	}
	public void setRestaurantMenuBizBeans(
			List<RestaurantMenuBizBean> restaurantMenuBizBeans) {
		this.restaurantMenuBizBeans = restaurantMenuBizBeans;
	}
	private RestaurantMenuService restaurantMenuService;
	
	@Resource
	public void setRestaurantMenuService(RestaurantMenuService restaurantMenuService) {
		this.restaurantMenuService = restaurantMenuService;
	}
	public RestaurantMenuBizBean getRestaurantMenuBizBean() {
		return restaurantMenuBizBean;
	}
	public void setRestaurantMenuBizBean(RestaurantMenuBizBean restaurantMenuBizBean) {
		this.restaurantMenuBizBean = restaurantMenuBizBean;
	}

	/**
	 * 获得餐厅菜单
	 * @return
	 */
	public String getRestaurantMenus(){
		if(queryPage == null){
			super.initPagination();
		}
		restaurantMenuBizBeans = restaurantMenuService.getRestaurantMenus(queryPage, restaurantMenuBizBean);
		return "getRestaurantMenus";
	}
	
	/**
	 * 修改页面
	 * @return
	 */
	public String getRestaurantMenuEdit(){
		if(null != restaurantMenuBizBeans && !restaurantMenuBizBeans.isEmpty()){
			for (int i = 0; i < restaurantMenuBizBeans.size(); i++) {
				RestaurantMenuBizBean restaurantMenuBizBean = restaurantMenuBizBeans.get(i);
				if(restaurantMenuBizBean != null && restaurantMenuBizBean.getId() != null){
					this.restaurantMenuBizBean = restaurantMenuService.getEntityById(restaurantMenuBizBean.getId());
					break;
				}
			}
			return "getRestaurantMenuEdit";
		}
		return getRestaurantMenus();
	}
	
	/**
	 * 修改
	 * @return
	 */
	public String restaurantMenuEdit(){
		if(restaurantMenuBizBean != null && restaurantMenuBizBean.getId() != null && checkData(restaurantMenuBizBean)){
			restaurantMenuService.execute(restaurantMenuBizBean, BaseService.COMMAND_UPDATE);
			super.setMessageID("SYS_MSG_00022");
		} else {
			if(null == restaurantMenuBizBeans){
				restaurantMenuBizBeans = new ArrayList<RestaurantMenuBizBean>();
			}
			restaurantMenuBizBeans.add(restaurantMenuBizBean);
			return getRestaurantMenuEdit();
		}
		super.setBackurl(request.getContextPath() + "/restaurantMenu/restaurantMenuAction!getRestaurantMenus.do");
		return "toMessage";
	}
	
	/**
	 * 数据检查
	 * @param restaurantMenuBizBean
	 * @return
	 */
	private boolean checkData(RestaurantMenuBizBean restaurantMenuBizBean) {
		boolean mark = true;
		if(restaurantMenuBizBean != null ){
			if(restaurantMenuBizBean.getRid() == null){
				mark = false;
				super.addFieldError("error", "系统异常，请刷新页面！");
			}
			if(StringUtils.isEmpty(restaurantMenuBizBean.getName())){
				mark = false;
				super.addFieldError("name", "菜单名称不能为空！");
			}
			if(restaurantMenuBizBean.getMenuPrice() == null || restaurantMenuBizBean.getMenuPrice() == 0){
				mark = false;
				super.addFieldError("menuPrice", "菜单价格不能为空！");
			}
		}
		return mark;
	}
	/**
	 * 增加页面
	 * @return
	 */
	public String getRestaurantMenuAdd(){
		return "getRestaurantMenuAdd";
	}
	
	/**
	 * 增加
	 * @return
	 */
	public String restaurantMenuAdd(){
		if(restaurantMenuBizBean != null  && checkData(restaurantMenuBizBean)){
			restaurantMenuService.execute(restaurantMenuBizBean, BaseService.COMMAND_ADD);
			super.setMessageID("SYS_MSG_00022");
		} else {
			return getRestaurantMenuAdd();
		}
		super.setBackurl(request.getContextPath() + "/restaurantMenu/restaurantMenuAction!getRestaurantMenus.do?restaurantMenuBizBean.rid="+restaurantMenuBizBean.getRid());
		return "toMessage";
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String restaurantMenuDelete(){
		if(null != restaurantMenuBizBeans && !restaurantMenuBizBeans.isEmpty()){
			restaurantMenuService.execute(restaurantMenuBizBeans, BaseService.COMMAND_DELETE);
			super.setMessageID("SYS_MSG_00022");
		} else {
			return getRestaurantMenus();
		}
		super.setBackurl(request.getContextPath() + "/restaurantMenu/restaurantMenuAction!getRestaurantMenus.do");
		return "toMessage";
	}
}
